package com.hongsheng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.hongsheng.pojo.Driver;
import com.hongsheng.pojo.R;
import com.hongsheng.service.DriverService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:DriverController
 * Description:
 *
 * @Author: 曾晋冠
 * @Version: 2022.3.2
 * @Create: 2023/8/10 - 16:42
 */
@RestController
@CrossOrigin
@RequestMapping("/driver")
public class DriverController {
    @Resource
    private DriverService driverService;
  Driver driver=new Driver();
    /**
     * 查询
     * @return
     */
    @GetMapping
    public R findAllDriver(){
        return new R(true, driverService.findAllDriver());
    }
    /**
     * 添加
     * @param driver
     * @return
     */
    @PostMapping
    public R addDriver(@RequestBody Driver driver){
        return new R(true,driverService.addDriver(driver)) ;
    }
    /**
     * 修改
     * @param driver
     * @return
     */
    @PutMapping
    public R updateDriver(@RequestBody Driver driver){
        if (driver.getType().equals("大型货车")) {
            driver.setCarLoad(30000.0);
            driver.setSize(60.0);
        } else if (driver.getType().equals("中型货车")) {
            driver.setCarLoad(20000.0);
            driver.setSize(40.0);
        } else if (driver.getType().equals("小型货车")) {
            driver.setCarLoad(10000.0);
            driver.setSize(20.0);
        }
        return new R(true,driverService.updateDriver(driver));
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R deleteDriver(@PathVariable Integer id){
        return new R(true,driverService.deleteDriver(id)) ;
    }
    @GetMapping("/{id}")
    public R findAllById(@PathVariable Integer id){
        return new R(true,driverService.findAllById(id));
    }
    @GetMapping("/{currentPage}/{pageSize}")
    public R getPageDriver(@PathVariable Integer currentPage,@PathVariable Integer pageSize,Driver driver){
        PageInfo<Driver> pageDriver = driverService.getPageDriver(currentPage,pageSize, driver);
        return new R(true,pageDriver);
    }

    @GetMapping("/findStatus/{id}")
    public R findStatus(@PathVariable Integer id){
        return new R(true,driverService.findStatus(id));
    }

    @GetMapping("/findPhone/{driverPhone}")
    public R findPhone(@PathVariable String driverPhone){
        Boolean phone = driverService.findPhone(driverPhone);
        return new R(true,phone);
    }
}
